<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '查看'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="商品名称" prop="goodsName">
      <el-input v-model="dataForm.goodsName" placeholder="商品名称"></el-input>
    </el-form-item>
    <el-form-item label="商品主图" prop="img">
      <el-upload
      :action="this.$http.adornUrl(`/business/class/uploadIcon?token=${this.$cookie.get('token')}`)"
      :limit="1"
      list-type="picture-card"
      :file-list="dataForm.img"
      :on-preview="handlePreview"
      :on-success="handleSuccess"
      :on-remove="handleRemove"
      :on-exceed="onExceed">
      <i class="el-icon-plus"></i>
      </el-upload>
      <el-dialog :visible.sync="dialogVisible" :append-to-body='true'>
        <img width="100%" :src="dialogImageUrl" alt="">
      </el-dialog>
      <!-- <el-input v-model="dataForm.img" placeholder=""></el-input> -->
    </el-form-item>
    <el-form-item label="商品轮播图" prop="rotationChart">
      <el-upload
      :action="this.$http.adornUrl(`/business/class/uploadIcon?token=${this.$cookie.get('token')}`)"
      :limit="4"
      list-type="picture-card"
      :file-list="dataForm.rotationChart"
      :on-preview="handlePreview1"
      :on-success="handleSuccess1"
      :on-remove="handleRemove1"
      :on-exceed="onExceed1">
      <i class="el-icon-plus"></i>
      </el-upload>
      <el-dialog :visible.sync="dialogVisible" :append-to-body='true'>
        <img width="100%" :src="dialogImageUrl" alt="">
      </el-dialog>
      <!-- <el-input v-model="dataForm.imgs" placeholder="商品点击数"></el-input> -->
    </el-form-item>
    <el-form-item label="添加时间" prop="addTime">
      <el-input v-model="dataForm.addTime" placeholder="商品详情"></el-input>
    </el-form-item>
    <el-form-item label="点击量" prop="goodsClick">
      <el-input v-model="dataForm.goodsClick" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="商品库存" prop="goodsInventory">
      <el-input v-model="dataForm.goodsInventory" placeholder="库存数量"></el-input>
    </el-form-item>
    <el-form-item label="seo关键字" prop="seoKeywords">
      <el-input v-model="dataForm.seoKeywords" placeholder="seo关键字"></el-input>
    </el-form-item>
    <el-form-item label="seo描述" prop="seoDescription">
      <el-input v-model="dataForm.seoDescription" placeholder="seo描述"></el-input>
    </el-form-item>
    <el-form-item label="库存警报" prop="warnNumber">
      <el-input v-model="dataForm.warnNumber" placeholder="库存警报"></el-input>
    </el-form-item>
    <el-form-item label="商品原价" prop="goodsPrice">
      <el-input v-model="dataForm.goodsPrice" placeholder="商品原价"></el-input>
    </el-form-item>
    <el-form-item label="商城售价" prop="vipPrice">
      <el-input v-model="dataForm.vipPrice" placeholder="商城售价"></el-input>
    </el-form-item>
    <el-form-item label="商品利润" prop="goodsCostingPrice">
      <el-input v-model="dataForm.goodsCostingPrice" placeholder="商品利润"></el-input>
    </el-form-item>
    <el-form-item label="销售数量" prop="goodsSaleNum;">
      <el-input v-model="dataForm.goodsSaleNum" placeholder="销售数量"></el-input>
    </el-form-item>
    <el-form-item label="产品型号" prop="goodsSerial">
      <el-input v-model="dataForm.goodsSerial" placeholder="产品型号"></el-input>
    </el-form-item>
    <el-form-item label="物流公司运费" prop="expressTransFee">
      <el-input v-model="dataForm.expressTransFee" placeholder="物流公司运费"></el-input>
    </el-form-item>
    <el-form-item label="商品简要" prop="goodsBrief">
      <el-input v-model="dataForm.goodsBrief" placeholder="商品简要"></el-input>
    </el-form-item>
    <el-form-item label="现金券" prop="integral">
      <el-input v-model="dataForm.integral" placeholder="现金券"></el-input>
    </el-form-item>
    <el-form-item label="价格类型" prop="priceType">
      <el-input v-model="dataForm.priceType" placeholder="价格类型"></el-input>
    </el-form-item>
    <el-form-item label="商品详情" prop="goodsDetails">
      <UEditor ref="ueditor"></UEditor>
      <!-- <el-input v-model="dataForm.goodsDetails" placeholder="商品详情"></el-input> -->
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import UEditor from '@/components/ueditor/ueditor.vue'
  export default {
    components: {
      UEditor
    },
    data () {
      return {
        isDisabled: false,
        visible: false,
        dialogImageUrl: '',
        dialogVisible: false,
        dataForm: {
          id: 0,
          goodsName: '',
          img: [],
          rotationChart: [],
          addTime: '',
          goodsClick: '',
          goodsInventory: '',
          seoKeywords: '',
          seoDescription: '',
          warnNumber: '',
          goodsPrice: '',
          vipPrice: '',
          goodsCostingPrice: '',
          goodsSerial: '',
          expressTransFee: '',
          goodsSaleNum: '',
          goodsBrief: '',
          integral: '',
          priceType: '',
          goodsDetails: '',
          IMG: '',
          photoId: '',
          accIds: []
        },
        dataRule: {}
      }
    },
    methods: {
      // 图片预览
      handlePreview (file) {
        this.dialogImageUrl = file.url
        this.dialogVisible = true
      },
      // 文件上传成功的钩子函数
      handleSuccess (res, file, fileList) {
        this.$message({
          type: 'info',
          message: '图片上传成功',
          duration: 6000
        })
        if (res.code === 0) {
          this.dataForm.img.push({id: this.dataForm.photoId, url: res.data[0].urlList[0]})
        }
        // console.log(this.dataForm.img)
      },
      // 删除文件之前的钩子函数
      handleRemove (file, fileList) {
        for (var i = 0; i < this.dataForm.img.length; i++) {
          var picc = this.dataForm.img[i]
          if (picc.url === file.url) {
            this.dataForm.img.splice(i, 1)
          }
        }
        this.$message({
          type: 'info',
          message: '已删除原有图片',
          duration: 6000
        })
      },
      // 上传的文件个数超出设定时触发的函数
      onExceed (files, fileList) {
        this.$message({
          type: 'info',
          message: '最多只能上传1个图片',
          duration: 6000
        })
      },
      // 图片预览
      handlePreview1 (file) {
        this.dialogImageUrl = file.url
        this.dialogVisible = true
      },
      // 文件上传成功的钩子函数
      handleSuccess1 (res, file, fileList) {
        this.$message({
          type: 'info',
          message: '图片上传成功',
          duration: 6000
        })
        if (res.code === 0) {
          this.dataForm.rotationChart.push({id: '', url: res.data[0].urlList[0]})
        }
        // console.log(this.dataForm.imgs)
      },
      // 删除文件之前的钩子函数
      handleRemove1 (file, fileList) {
        this.dataForm.accIds.push({id: file.id, url: ''})
        console.log(file.id)
        for (var i = 0; i < this.dataForm.rotationChart.length; i++) {
          var picc = this.dataForm.rotationChart[i]
          if (picc.url === file.url) {
            this.dataForm.rotationChart.splice(i, 1)
          }
        }
        // console.log(this.dataForm.imgs)
        this.$message({
          type: 'info',
          message: '已删除原有图片',
          duration: 6000
        })
      },
      // 上传的文件个数超出设定时触发的函数
      onExceed1 (files, fileList) {
        this.$message({
          type: 'info',
          message: '最多只能上传4个图片',
          duration: 6000
        })
      },
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.isDisabled = false
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/goods/getGoodsById/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.id = data.data.id
                this.dataForm.photoId = data.data.photoId
                this.dataForm.goodsName = data.data.goodsName
                // this.dataForm.img = data.data.img
                var pic = data.data.img
                this.dataForm.img.push({id: data.data.photoId, url: pic})
                // this.dataForm.imgs = data.data.imgs
                var pic1 = data.data.rotationChart
                for (var i = 0; i < pic1.length; i++) {
                  this.dataForm.IMG = pic1[i].id
                  this.dataForm.rotationChart.push({id: pic1[i].id, url: pic1[i].imgSrc})
                }
                this.dataForm.addTime = data.data.addTime
                this.dataForm.goodsClick = data.data.goodsClick
                this.dataForm.goodsInventory = data.data.goodsInventory
                this.dataForm.seoKeywords = data.data.seoKeywords
                this.dataForm.seoDescription = data.data.seoDescription
                this.dataForm.warnNumber = data.data.warnNumber
                this.dataForm.goodsPrice = data.data.goodsPrice
                this.dataForm.vipPrice = data.data.vipPrice
                this.dataForm.goodsCostingPrice = data.data.goodsCostingPrice
                this.dataForm.goodsSerial = data.data.goodsSerial
                this.dataForm.expressTransFee = data.data.expressTransFee
                this.dataForm.goodsBrief = data.data.goodsBrief
                this.dataForm.integral = data.data.integral
                this.dataForm.priceType = data.data.priceType
                this.ueSetContent(data.data.goodsDetails)
                // this.dataForm.goodsDetails = data.data.goodsDetails
                this.dataForm.goodsSaleNum = data.data.goodsSaleNum
              }
            })
          }
        })
      },
      // 获取文档内容
      ueGetContent: function () {
        return this.$refs.ueditor.getUEContent()
      },
      ueSetContent: function (content) {
        this.$refs.ueditor.setUEContent(content)
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.isDisabled = true
            this.dataForm.goodsDetails = this.ueGetContent()
            this.$http({
              url: this.$http.adornUrl(`/goods/${!this.dataForm.id ? 'save' : 'modifyGoodsById'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'goodsName': this.dataForm.goodsName,
                'img': this.dataForm.img,
                'rotationChart': this.dataForm.rotationChart,
                'addTime': this.dataForm.addTime,
                'goodsClick': this.dataForm.goodsClick,
                'goodsInventory': this.dataForm.goodsInventory,
                'seoKeywords': this.dataForm.seoKeywords,
                'seoDescription': this.dataForm.seoDescription,
                'warnNumber': this.dataForm.warnNumber,
                'goodsPrice': this.dataForm.goodsPrice,
                'vipPrice': this.dataForm.vipPrice,
                'goodsCostingPrice': this.dataForm.goodsCostingPrice,
                'goodsSerial': this.dataForm.goodsSerial,
                'expressTransFee': this.dataForm.expressTransFee,
                'goodsBrief': this.dataForm.goodsBrief,
                'integral': this.dataForm.integral,
                'priceType': this.dataForm.priceType,
                'goodsDetails': this.dataForm.goodsDetails,
                'goodsSaleNum': this.dataForm.goodsSaleNum,
                'accIds': this.dataForm.accIds,
                'photoId': this.dataForm.photoId
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
